package com.heimao.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.heimao.pojo.User;
import com.heimao.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@Controller
@RestController //该注解表示该类下的所有controller都返回字符串
public class UserController {



    //    @ResponseBody  单个方法使用该注解返回字符串
    @RequestMapping(value="/getUser",produces="application/json;charset=utf-8")
    public String getUser() throws JsonProcessingException {
        Date date=new Date();
        User user=new User(1,"黑猫",23,date);
        ObjectMapper mp=new ObjectMapper();
        String str= mp.writeValueAsString(user);
        return str;
    }

//    @ResponseBody
    //使用springMvc 全局转码汉字
    @RequestMapping(value="/getUser2")
    public String getUser2() throws JsonProcessingException {
        List<User> list=new ArrayList<User>();
        Date date=new Date();
        User user1=new User(1,"黑猫1",23,date);
        User user2=new User(2,"黑猫2",23,date);
        User user3=new User(3,"黑猫3",23,date);
        User user4=new User(4,"黑猫4",23,date);
        User user5=new User(5,"黑猫5",23,date);
        list.add(user1);
        list.add(user2);
        list.add(user4);
        list.add(user3);
        list.add(user5);
        ObjectMapper mp=new ObjectMapper();
        String str= mp.writeValueAsString(list);
        return str;
    }
    //使用java原生方式转化时间戳
    @RequestMapping(value="/javaTime")
    public String javaTime() throws JsonProcessingException {
        Date date=new Date();
        SimpleDateFormat fm=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        String dateStr=fm.format(date);
        ObjectMapper mp=new ObjectMapper();
        String str= mp.writeValueAsString(dateStr);
        return str;
    }
    //使用jackson 的时间格式转化时间戳
    @RequestMapping(value="/maperTime")
    public String maperTime() throws JsonProcessingException {
        Date date=new Date();
        ObjectMapper mp=new ObjectMapper();
        mp.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        SimpleDateFormat fm=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        mp.setDateFormat(fm);
        User user=new User(1,"黑猫1",23,date);
        String str= mp.writeValueAsString(user);
        return str;
    }

    //将代码封装成工具类
    @RequestMapping(value="/jsonUtil")
    public String JsonUtil(){
        Date date=new Date();
        User user=new User(1,"黑猫1",23,date);
        return JsonUtil.objectAsJson(user);
    }

    @RequestMapping(value="/aliJson")
    public String aliJson(){
        Date date=new Date();
        User user=new User(1,"黑猫1",23,date);
        String str= JSON.toJSONString(user);
        return str;
    }


}
